<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <!-- 引入样式 -->
    <script src="../../js/vue.js"></script>
    <script src="../../js/axios.min.js"></script>
    <script src="../../js/dayjs.min.js"></script>
    <link rel="stylesheet" href="../../plugins/element_UI/index.css">
    <script src="../../plugins/element_UI/index.js"></script>
</head>
<body>
<div id="app">
    <el-row class="el-search-row-d" style="margin-right: auto">
<!--        <el-input-->
<!--                placeholder="姓名模糊查询"-->
<!--                v-model="sffName"-->
<!--                style="width: fit-content"-->
<!--                @change="init"-->
<!--                clearable>-->
<!--        </el-input>-->
<!--        <el-button size="small" type="primary" @click="init">检索</el-button>-->
        <el-button type="primary"  @click="dialogFormVisible = true">新增</el-button>
    </el-row>
    <el-dialog  :close-on-click-modal="false"  title="添加/修正" :visible.sync="dialogFormVisible" @close="closeDiglog" :close-on-click-modal="false">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">

            <el-form-item label="职工姓名" prop="sffName">
                <el-input v-model="ruleForm.sffName " type="text" placeholder="请输入职工姓名" maxlength="20"
                          show-word-limit></el-input>
            </el-form-item>

            <el-form-item label="角色" prop="sffCardType">
                <el-select v-model="ruleForm.sffCardType" placeholder="请选择" :disabled="isEdit">
                    <el-option label="超级管理员" value=2 :hidden="isHospitalType"></el-option>
                    <el-option label="管理员"  value=0 ></el-option>
                    <el-option label="后台预约" value=1 ></el-option>
                    <el-option label="排班维护" value=3 ></el-option>
                </el-select>
            </el-form-item>

            <el-form-item label="所属医院" prop="hospitalId" :hidden="isHospitalHidden">
                <el-select v-model="ruleForm.hospitalId" filterable placeholder="请选择" :disabled="isHospitalEdit" >
                    <el-option
                            v-for="item in hospitals"
                            :key="item.id"
                            :label="item.hosName"
                            :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>

            <el-form-item label="登录工号" prop="sffLoginNum">
                <el-input v-model="ruleForm.sffLoginNum"  type="text" placeholder="请输入登录工号" maxlength="20"
                          show-word-limit ></el-input>
            </el-form-item>

            <el-form-item label="登录密码" prop="password">
                <el-input v-model="ruleForm.password" type="text" placeholder="请输入登录密码" maxlength="20"
                          show-word-limit ></el-input>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')" style="align-content: center">保存</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
    <template>
        <el-table
                :data="tableData"
                style="width: 100%"
                :cell-style="childdreCellStyle"
                @current-change="handleCurrentChange"
                max-height="800">
            <el-table-column
                    fixed
                    prop="sffName"
                    label="姓名"
                    width="120">
            </el-table-column>

            <el-table-column
                    prop="sffCardType"
                    label="角色"
                    :formatter="formatterSex"
                    width="120">
            </el-table-column>

            <el-table-column
                    prop="sffLoginNum"
                    label="登录工号"
                    width="120">
            </el-table-column>
            <el-table-column
                    prop="password"
                    label="用户密码"
                    width="120">
            </el-table-column>
            <el-table-column
                    prop="hospitalId"
                    label="所属医院"
                    sortable
                    :formatter="formatterHospital"
                    width="120">
            </el-table-column>
            <el-table-column
                    label="操作"
                    width="120">
                <template slot-scope="scope">
                    <el-button
                            @click.native.prevent="deleteRow(scope.$index,scope.row)"
                            type="text"
                            size="small">
                        <p v-if="scope.row.status=='0' || scope.row.status==0 ">
                            注销
                        </p>
                        <p v-else>
                            还原
                        </p>
                    </el-button>
                    <el-button
                            @click.native.prevent="editRow(scope.$index,scope.row)"
                            type="text"
                            size="small">
                        修改
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
    </template>
</div>
</body>
<script>
    let app = new Vue({
        el: '#app',
        data() {
            return {
                tableData: [],
                sffName: "",
                status: 0,
                isEdit:false,
                isHospitalHidden:false,
                isHospitalType:false,
                isHospitalEdit:false,
                loading: false,
                hospitalId: JSON.parse(localStorage.getItem("user")).hospitalId ,
                currentUpRow: null,
                hospitals: [],
                ruleForm: {
                    appId: null,
                    chinaSpell: "",
                    fiveStroke: "",
                    hospitalId: JSON.parse(localStorage.getItem("user")).hospitalId ,
                    id: null,
                    password: "",
                    sffBirthday: "",
                    sffCardInfo: "",
                    sffCardType: "1",
                    sffInputCode: null,
                    sffLoginNum: "",
                    sffName: "",
                    sffSex: "1",
                    timeStamp: null,
                    token: null
                },
                rules: {
                    sffName: [
                        { required: true, message: '请输入职工姓名', trigger: 'blur' },
                        { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
                    ],password: [
                        { required: true, message: '请输入登录密码', trigger: 'blur' },
                        { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
                    ],
                    sffLoginNum: [
                        { required: true, message: '请输入登录工号', trigger: 'blur' },
                        { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
                    ], sffCardType: [
                        { required: true, message: '请选择角色', trigger: 'change' }
                    ],
                },
                dialogTableVisible: false,
                dialogFormVisible: false,
                formLabelWidth: '120px'
            }
        },
        methods: {
            init: function () {
                let user=JSON.parse(localStorage.getItem("user"))
                if (user.sffCardType=="2"){
                    this.isEdit=false
                    this.isHospitalHidden=true;
                    this.isHospitalEdit=false;
                    this.isHospitalType=false;
                }else if (user.sffCardType=="0"){
                    this.isEdit=false
                    this.isHospitalHidden=false;
                    this.isHospitalType=true;
                    this.isHospitalEdit=true;
                }else if (user.sffCardType=="1"){
                    this.isEdit=false
                    this.isHospitalHidden=false;
                    this.isHospitalType=false;
                    this.isHospitalEdit=true;
                }else if (user.sffCardType=="3"){
                    this.isEdit=false
                    this.isHospitalHidden=false;
                    this.isHospitalType=false;
                    this.isHospitalEdit=true;
                }
                axios.post("http://" + window.location.host + '/hospital/select_list', {
                    hosName: '',hospitalId: this.hospitalId
                }).then(res => {
                    this.hospitals = res.data.data;
                }).catch((e) => {
                    alert('获取数据失败');
                });
                const loading = this.$loading({
                    lock: true,
                    text: 'Loading',
                    spinner: 'el-icon-loading',
                    background: 'rgba(0, 0, 0, 0.7)'
                });
                let that = this
                that.loading=false
                axios.post("http://"+window.location.host+'/pubStaffInfo/select_list', {userName: this.sffName,hospitalId: this.hospitalId}).then(res => {
                    that.tableData = res.data.data;
                    loading.close()
                }).catch((e) => {
                    alert('获取数据失败');
                    loading.close()
                });
            },
            formatterSex: function (row, column) {
                if (row.sffCardType == 1 || row.sffCardType == '1') {
                    return "后台预约"
                } else  if (row.sffCardType == 2 || row.sffCardType == '2') {
                    return "超级管理员"
                }else  if (row.sffCardType == 0 || row.sffCardType == '0') {
                    return "管理员"
                }else  if (row.sffCardType == 3 || row.sffCardType == '3') {
                    return "排班维护"
                }
            },
            handleCurrentChange(val) {
                this.currentUpRow = val;
                console.log(this.currentUpRow)
            },
            closeDiglog: function () {
                this.ruleForm ={
                    appId: null,
                        chinaSpell: "",
                        fiveStroke: "",
                        hospitalId: JSON.parse(localStorage.getItem("user")).hospitalId ,
                        id: null,
                        password: "",
                        sffBirthday: "",
                        sffCardInfo: "",
                        sffCardType: "1",
                        sffInputCode: null,
                        sffLoginNum: "",
                        sffName: "",
                        sffSex: "1",
                        timeStamp: null,
                        token: null
                }
            },
            submitForm: function(ruleForm){
                this.$refs[ruleForm].validate((valid) => {
                    if (valid) {
                        let url = "";
                        if (this.ruleForm.sffCardType == 2 || this.ruleForm.sffCardType == '2'){
                            this.ruleForm.hospitalId=''
                        }
                        let msg=""
                        if (this.ruleForm.id){
                            url="/pubStaffInfo/update_by_primary_key"
                            msg="更新成功！"
                        }else {
                            url="/pubStaffInfo/create";
                            msg="新增成功！"
                        }
                        axios.post("http://"+window.location.host+url, this.ruleForm).then(res => {
                            if (res.data.code==500){
                                this.$message({
                                    showClose: true,
                                    type: 'error',
                                    message: res.data.msg
                                });
                            }else {
                                this.$refs[ruleForm].resetFields();
                                this.dialogFormVisible = false
                                this.init()
                                this.$message({
                                    type: 'success',
                                    message: msg
                                });
                            }
                        }).catch(function (error) {
                            alert(error.response.data.message)
                        });
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });

            },
            formatterHospital: function (row, column) {
                for (let i = 0; i < this.hospitals.length; i++) {
                    if (row.hospitalId == this.hospitals[i].id) {
                        return this.hospitals[i].hosName
                    }
                }
            },
            createDepartment: function(){
                let params = new URLSearchParams();
                params.append('startDate', startDate);
                params.append('endDate', endDate);
                axios.post("http://"+window.location.host+'/pubDepartments/select_list', params).then(res => {
                    if (res.status == 500 || res.status == '500'){
                        alert(res.message);
                    }
                    this.init()
                }).catch(function (error) {
                    loading.close();
                    alert(error.response.data.message)
                });
            },
            deleteRow: function(index, row) {
                let status = row.status=='0'?'1':'0'
                axios.post("http://"+window.location.host+'/pubStaffInfo/update_by_primary_key_selective',
                    {'id':row.id,'status':status}
                ).then(res => {
                    alert("操作成功！");
                    this.init()
                    this.ruleForm = {
                        appId: null,
                            chinaSpell: "",
                            fiveStroke: "",
                            hospitalId: null,
                            id: null,
                            password: "",
                            sffBirthday: "",
                            sffCardInfo: "",
                            sffCardType: "1",
                            sffInputCode: null,
                            sffLoginNum: "",
                            sffName: "",
                            sffSex: "1",
                            timeStamp: null,
                            token: null
                    }
                })
            },
            editRow:function(index, row) {
                this.ruleForm=row;
                this.dialogFormVisible = true
            },
            //设置指定行、列、具体单元格颜色
            childdreCellStyle: function (row, column, rowIndex, columnIndex) {
                if (row.row.status == 1 || row.row.status == '1' ) {
                    return 'color:red' //rgb(105,0,7)
                } else {
                    return '' //rgb(105,0,7)
                }
            }
        },
        watch: {
            'ruleForm.sffCardType': function (val, oldval) {
                let user=JSON.parse(localStorage.getItem("user"))
                if (user.sffCardType=="2"){
                    if (val=="2" ){
                        this.isHospitalEdit=true;
                        this.isHospitalHidden=true;
                    }else {
                        this.isHospitalEdit=false;
                        this.isHospitalHidden=false;
                    }
                }else {
                    this.isHospitalEdit=true;
                }

            }
        },
        created() {
            this.init()
        },
        mounted() {
        }
    });
</script>

</html>
